package com.wzy.sharepreferenceutil;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private TextView mShowText;
    private Button mPutSpBtn, mGetSpBtn;
    private EditText mInput;

    public static final String SHARE_PREFERENCE_KEY = "com.wzy.spkey.SHARE_PREFERENCE_KEY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        mShowText = (TextView) findViewById(R.id.show);
        mPutSpBtn = (Button) findViewById(R.id.put_preference);
        mGetSpBtn = (Button) findViewById(R.id.get_preference);
        mInput = (EditText) findViewById(R.id.input);
        mPutSpBtn.setOnClickListener(this);
        mGetSpBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.put_preference:
                String s = mInput.getText().toString();
                if (s.isEmpty()) {
                    s = "EditText is empty";
                }
                SettingSp.getInstance(mContext).putString(SHARE_PREFERENCE_KEY, s);
                break;
            case R.id.get_preference:
                String ss = SettingSp.getInstance(mContext).getString(SHARE_PREFERENCE_KEY, "default value");
                mShowText.setText(ss);
                break;
        }
    }
}
